/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
 *
 * @file    msg_vgs.c
 * @brief   msg vgs function
 * @author  HiMobileCam NDK develop team
 * @date  2019-8-8
 */
#include "hi_type.h"
#include "hi_ipcmsg.h"
#include "msg_define.h"
#include "msg_vgs.h"
#include "hi_mapi_vgs.h"
#include "hi_mapi_vgs_define.h"
#include "msg_server.h"
#include "mapi_comm_inner.h"

#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif /* End of #ifdef __cplusplus */

static HI_S32 MSG_VGS_BeginJob(HI_S32 siId, HI_IPCMSG_MESSAGE_S *msg)
{
    HI_HANDLE vgsHdl;
    HI_S32 ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    ret = HI_MAPI_VGS_BeginJob(&vgsHdl);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "HI_MAPI_VGS_BeginJob Failed : %#x!\n", ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(msg, ret, &vgsHdl, sizeof(HI_HANDLE));
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_BeginJob HI_IPCMSG_CreateRespMessage fail\n");
    }

    ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_BeginJob call HI_IPCMSG_SendAsync fail,ret:%x\n", ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return HI_SUCCESS;
}

static HI_S32 MSG_VGS_AddScaleTask(HI_S32 siId, HI_IPCMSG_MESSAGE_S *msg)
{
    HI_HANDLE handle;
    handle = msg->as32PrivData[0];
    HI_S32 ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    ret = HI_MAPI_VGS_AddScaleTask(handle, (HI_MAPI_VGS_ScaleTaskAttr *)msg->pBody);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "HI_MAPI_VGS_AddScaleTask Failed : %#x!\n", ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(msg, ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_AddScaleTask call HI_IPCMSG_CreateRespMessage fail\n");
    }

    ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_AddScaleTask call HI_IPCMSG_SendAsync fail,ret:%x\n", ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);

    return HI_SUCCESS;
}

static HI_S32 MSG_VGS_EndJob(HI_S32 siId, HI_IPCMSG_MESSAGE_S *msg)
{
    HI_HANDLE vgsHdl;
    vgsHdl = *(HI_HANDLE *)msg->pBody;
    HI_S32 ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    ret = HI_MAPI_VGS_EndJob(vgsHdl);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "HI_MAPI_VGS_EndJob Failed : %#x!\n", ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(msg, ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_EndJob call HI_IPCMSG_CreateRespMessage fail\n");
    }

    ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_EndJob call HI_IPCMSG_SendAsync fail,ret:%x\n", ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return HI_SUCCESS;
}

static HI_S32 MSG_VGS_CanceJob(HI_S32 siId, HI_IPCMSG_MESSAGE_S *msg)
{
    HI_HANDLE vgsHdl;
    vgsHdl = *(HI_HANDLE *)msg->pBody;
    HI_S32 ret;
    HI_IPCMSG_MESSAGE_S *respMsg = HI_NULL;

    ret = HI_MAPI_VGS_CanceJob(vgsHdl);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "HI_MAPI_VGS_CanceJob Failed : %#x!\n", ret);
    }

    respMsg = HI_IPCMSG_CreateRespMessage(msg, ret, HI_NULL, 0);
    if (respMsg == HI_NULL) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_CanceJob call HI_IPCMSG_CreateRespMessage fail\n");
    }

    ret = HI_IPCMSG_SendAsync(siId, respMsg, HI_NULL);
    if (ret != HI_SUCCESS) {
        MAPI_ERR_TRACE(HI_MAPI_MOD_VGS, "VGS_CanceJob call HI_IPCMSG_SendAsync fail,ret:%x\n", ret);
        HI_IPCMSG_DestroyMessage(respMsg);
        return ret;
    }

    HI_IPCMSG_DestroyMessage(respMsg);
    return HI_SUCCESS;
}

static MSG_MODULE_CMD_S g_stModuleCmdTable[] = {
    { MSG_CMD_VGS_BEGIN_JOB,                MSG_VGS_BeginJob },
    { MSG_CMD_VGS_ADD_SCALETASK,            MSG_VGS_AddScaleTask},
    { MSG_CMD_VGS_END_JOB,                  MSG_VGS_EndJob },
    { MSG_CMD_VGS_CANCE_JOB,                MSG_VGS_CanceJob },
};

MSG_SERVER_MODULE_S g_stModuleVgs = {
    HI_MAPI_MOD_VGS,
    "vgs",
    sizeof(g_stModuleCmdTable) / sizeof(MSG_MODULE_CMD_S),
    &g_stModuleCmdTable[0]
};

MSG_SERVER_MODULE_S *MAPI_MSG_GetVgsMod(HI_VOID)
{
    return &g_stModuleVgs;
}

#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* End of #ifdef __cplusplus */
